﻿///  Copyright (c) 2010 Ray Liang (http://www.dotnetage.com)
///  Dual licensed under the MIT and GPL licenses:
///  http://www.opensource.org/licenses/mit-license.php
///  http://www.gnu.org/licenses/gpl.html

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Web.Script;
using DNA.UI.JQuery.Design;

namespace DNA.UI.JQuery
{
    [JQuery(Name = "button", Assembly = "jQuery", DisposeMethod = "destroy",
       ScriptResources = new string[] { "ui.core.js", "ui.widget.js", "ui.button.js"}, StartEvent = ClientRegisterEvents.DocumentReady)]
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [System.Drawing.ToolboxBitmap(typeof(DatePicker), "ButtonEx.ButtonEx.ico")]
    [ToolboxData("<{0}:ButtonEx runat=\"server\" ID=\"ButtonEx1\"></{0}:ButtonEx>")]
    [ParseChildren(true)]
    [Designer(typeof(ButtonDesigner))]
    public class ButtonEx:Button
    {
        private bool showText=true;

        [JQueryOption("text",IgnoreValue=true)]
        public bool ShowText 
        {
            get { return showText; }
            set { showText = value; }
        }

        public string LeftIconCssClass { get; set; }

        public string RightIconCssClass { get; set; }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Button;
                //return base.TagKey;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            var builder = new JQueryScriptBuilder(this);
            if (!string.IsNullOrEmpty(Text))
                builder.AddOption("label", Text, true);

            if ((!string.IsNullOrEmpty(LeftIconCssClass)) || (!string.IsNullOrEmpty(RightIconCssClass)))
            {
                string _iconStr="{";
                _iconStr += "primary:" + (string.IsNullOrEmpty(LeftIconCssClass) ? "null" : "'" + LeftIconCssClass + "'") + ",";
                _iconStr += "secondary:"+(string.IsNullOrEmpty(RightIconCssClass) ? "null":"'"+RightIconCssClass+"'")+"}";
                builder.AddOption("icons", _iconStr);
            }

            ClientScriptManager.RegisterJQueryControl(this, builder);
        }
    }
}
